package org.example.c2;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author liu-rui
 * @date 2020/5/29 上午10:56
 * @description 线程安全问题，无法停止的worker
 * @since
 */
@Slf4j
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Worker w = new Worker();

        w.start();
        TimeUnit.SECONDS.sleep(5);
        log.info("send stop command");
        w.stop();
    }

    static class Worker {
        private boolean running = false;

        public void start() {
            running = true;
            new Thread(() -> {
                log.info("begin..");
                while (running) {

                }
                log.info("stopped");
            }, "t1").start();
        }

        public void stop() {
            running = false;
        }
    }
}
